package com.weike.lib.xxx;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;

public class LPrintPanel extends JPanel implements Printable {
	//打印预览技术  自己画图打印
	private JButton printButton;//打印按钮
	private JButton previewButton;//预览按钮
	private JPanel controlPanel;
	private Image img;
	private Dimension size;
	private PrinterJob job;//打印控制类
	private JFrame previewDialog;
	PageFormat pf;//打印任务格式

	public LPrintPanel() {
		super();
		setLayout(new BorderLayout());
		setBackground(Color.WHITE);
		addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseDragged(MouseEvent e) {
				Point point = e.getPoint();
				Graphics2D g2 = (Graphics2D) img.getGraphics();
				g2.setStroke(new BasicStroke(5));
				g2.setColor(Color.red);
				g2.drawLine(point.x, point.y, point.x, point.y);
				img.flush();
				repaint();
			}
		});
		add(getControlPanel(), BorderLayout.SOUTH);
		pf = new PageFormat();
		pf.setOrientation(PageFormat.LANDSCAPE);//
		job = PrinterJob.getPrinterJob();


		previewDialog = new JFrame();
		previewDialog.setTitle("第二张打印预览页面");
		previewDialog.setSize(800, 600);
	}
	private void drawPage(Graphics2D g2) {
		g2.drawImage(img, 0, 0, this);
	}
	protected JButton getPreviewButton() {
		if (previewButton == null) {
			previewButton = new JButton();
			previewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					pf = job.pageDialog(pf);
					LMyCanvas canvas = new LMyCanvas(pf,img);
					JScrollPane spanel = new JScrollPane(canvas);
					previewDialog.getContentPane().add(spanel);
					previewDialog.setVisible(true);
					canvas.repaint();
				}
			});
			previewButton.setText("打印预览");
		}
		return previewButton;
	}
	protected JButton getPrintButton() {
		if (printButton == null) {
			printButton = new JButton();
			printButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						job.setPrintable(LPrintPanel.this);
						job.setJobName("打印图形");
						job.print();
					} catch (PrinterException e1) {
						e1.printStackTrace();
					}
				}
			});
			printButton.setText("打印");
		}
		return printButton;
	}

	protected JPanel getControlPanel() {
		if (controlPanel == null) {
			controlPanel = new JPanel();
			controlPanel.setBorder(new LineBorder(Color.BLUE, 1, false));
			final FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(20);
			controlPanel.setLayout(flowLayout);
			controlPanel.add(getPreviewButton());
			controlPanel.add(getPrintButton());
		}
		return controlPanel;
	}
	@Override
	protected void paintComponent(Graphics g) {
		if (size == null) {
			size = getSize();
			img = createImage(size.width, size.height);
		}
		Graphics2D g2 = (Graphics2D) g;
		drawPage(g2);
	}
	//实现打印页面Printable接口  重写print方法
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)throws PrinterException {
		if (pageIndex > 0)
			return Printable.NO_SUCH_PAGE;
		int x = (int) pageFormat.getImageableX();
		int y = (int) pageFormat.getImageableY();
		Graphics2D g2 = (Graphics2D) graphics;
		g2.translate(x, y);
		drawPage(g2);
		return Printable.PAGE_EXISTS;
	}


	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setTitle("第一页面");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600, 480);
		frame.setLocationRelativeTo(null);
		LPrintPanel canvas = new LPrintPanel();//添加打印预览的pannal
		frame.add(canvas);
		frame.setVisible(true);
	}
}